import requests
from wordpress_project.base.usrn_em_rand import random_username  # 随机10位字母加数字
from wordpress_project.base.usrn_em_rand import random_email  # 随机7位字母加数字邮箱格式
from Usersystem.md5 import make_md5

# 建立会话
s = requests.session()

# 注册
username = random_username()
email = random_email()
password = "admin123"
register_url = 'http://114.132.200.42:3456/user/register'
mydata = {
    "username": username,
    "password": password,
    "cpassword": password,
    "checkcode": "1234",
    "email": email
}
res = s.post(url=register_url, data=mydata)
print(f'注册：{res.json()}')

# 登录
url = 'http://114.132.200.42:3456/user/login'
data = {
    "username": username,
    "password": make_md5(password),
    "checkcode": "1234"
}
res = s.post(url=url, data=data)
a = res.json()
print(f'登录：{res.json()}')
get_userid = a['userId']
get_usertoken = a['userToken']

# 创建任务
creat_url = 'http://114.132.200.42:3456/task/info'
creat_data = {"title": "hahahahah"}
db = {'userId': get_userid, 'userToken': get_usertoken}
creat_res = s.post(url=creat_url, data=creat_data, headers=db)
print(f'创建任务：{creat_res.json()}')
get_task_id = creat_res.json()['taskid']

# 查询任务
query_data = {"status": "0"}
query_res = s.get(url=creat_url, params=query_data, headers=db)
print(f'查询任务：{query_res.json()}')

# 完成任务
complete_data = {'task_id': get_task_id}
complete_res = s.put(url=creat_url, data=complete_data, headers=db)
print(f'完成任务：{complete_res.json()}')

# 取消完成任务
complete_res = s.put(url=creat_url, data=complete_data, headers=db)
print(f'取消完成任务：{complete_res.json()}')

# 删除任务
delete_res = s.delete(url=creat_url, data=complete_data, headers=db)
print(f'删除任务：{delete_res.json()}')

# 上传头像
img_Url = 'http://114.132.200.42:3456/user/uploal_action'
my_img = [
    ('pic', ('gtl.png', open('gtl.png', 'rb')))
]
res = s.post(url=img_Url, files=my_img, headers=db)  # 关于文件上传的接口 files 表示
print(f'上传头像：{res.json()}')
